using System;
using System.Collections.Generic;
using System.Linq;
using Lucene.Linq.Mapping;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Lucene.Linq.VSTests.Mapping
{
    [TestClass]
    public class TestAttributeReader
    {
        [TestMethod]
        public void Document_and_field_details_are_read_from_class()
        {
            Type t = typeof (SampleClass);
            DocumentDetails dd = t.ReadDocumentDetails();
            Assert.IsNotNull(dd);
            IEnumerable<FieldDetails> fd = t.ReadFieldDetails();


            Assert.IsTrue(fd.Count() == 3);

            Func<IEnumerable<FieldDetails>, string, bool> contains = (list, name) => (from f in list
                                                                                      where f.Name == name
                                                                                      select f).Any();

            Assert.IsTrue(contains(fd, "A"));
            Assert.IsTrue(contains(fd, "B"));
            Assert.IsTrue(contains(fd, "C"));
        }


        [TestMethod]
        public void Document_and_field_details_are_read_from_metadata_class()
        {
            Type t = typeof (RealClass);
            DocumentDetails dd = t.ReadDocumentDetails();
            Assert.IsNotNull(dd);
            IEnumerable<FieldDetails> fd = t.ReadFieldDetails();


            Assert.IsTrue(fd.Count() == 3);

            Func<IEnumerable<FieldDetails>, string, bool> contains = (list, name) => (from f in list
                                                                                      where f.Name == name
                                                                                      select f).Any();

            Assert.IsTrue(contains(fd, "A"));
            Assert.IsTrue(contains(fd, "B"));
            Assert.IsTrue(contains(fd, "C"));
        }

        #region Nested type: RealClass

        [Document(MetadataType = typeof (RealMetaDataClass))]
        private class RealClass
        {
            public string A { get; set; }
            public string B { get; set; }
            public string C { get; set; }
        }

        #endregion

        #region Nested type: RealMetaDataClass

        private class RealMetaDataClass
        {
            [Field]
            public object A { get; set; }

            [Field]
            public object B { get; set; }

            [Field]
            public object C { get; set; }
        }

        #endregion

        #region Nested type: SampleClass

        [Document]
        private class SampleClass
        {
            [Field]
            public string A { get; set; }

            [Field]
            public string B { get; set; }

            [Field]
            public string C { get; set; }
        }

        #endregion
    }
}